﻿var origForeachUpdate = ko.bindingHandlers.foreach.update;
ko.bindingHandlers.foreach.update = function (element, valueAccessor, allBindingsAccessor, data, context) {

    var options = valueAccessor();

    if (options.defaultText) {

        var hasDefaultText = $(element).find('span.injected').length > 0;

        var array = ko.unwrap(options.data);

        if (!array || !array.length) {
            element.innerHTML = options.defaultText;
        } else if (hasDefaultText) {
            $(element).find('span.injected').remove();
        }
    }
    origForeachUpdate.apply(this, arguments);
};